package com.yinhai.mycat_test.controller;

import com.yinhai.mycat_test.VO.UserVO;
import com.yinhai.mycat_test.entity.User;
import com.yinhai.mycat_test.service.read.UserReadService;
import com.yinhai.mycat_test.service.write.UserWriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(value = "用户操作Api", tags = "用户信息的基本操作")
public class UserController {
    @Resource
    UserReadService userReadService;

    @Resource
    UserWriteService userWriteService;

    @GetMapping("/queryOneUser")
    public User queryOneUser(@Param("userId") String userId) {
        return userReadService.queryOneUser(userId);
    }

    @GetMapping("/queryUserAndIDCard")
    UserVO queryUserAndIDCard(@Param("userId") String userId) {
        return userReadService.queryUserAndIDCard(userId);
    }

    @GetMapping("/queryAllUser")
    List<User> queryAllUser() {
        return userReadService.queryAllUser();
    }

    @GetMapping("/queryUserByGender")
    List<User> queryUserByGender(String gender) {

        return userReadService.queryUserByGender("male");
    }

    @GetMapping("/queryUserIdLike01")
    List<User> queryUserIdLike01(@Param("id") String id) {
        return userReadService.queryUserIdLike01(id);
    }

    @GetMapping("/queryUserNameLike02")
    List<User> queryUserNameLike02(@Param("name") String name) {
        return userReadService.queryUserIdLike01(name);
    }

    @ApiOperation(value = "查询用户总数")
    @GetMapping("/queryUserCount")
    Integer queryUserCount() {
        return userReadService.queryUserCount();
    }

    @PostMapping("/insertUser")
    public void insertUser() {
        User user = null;
        for (int i = 1; i < 100; i++) {
            user = new User();
            user.setUserId(i);
            user.setUserName("jiangnan" + i);
            user.setAge(15);
            user.setEmail("ljnwsbs@163.com");
            user.setGender("male");
            user.setPhoneNumber("1234567890");
            userWriteService.insertUser(user);
        }
    }

    @PostMapping("/deleteAllUser")
    public void deleteAllUser() {
        userWriteService.deleteAllUser();
    }

    @PostMapping("/batchInsertUser")
    public void batchInsertUser() {
        User user =null;
        List<User> list =new LinkedList<>();
        for (int i = 1; i < 10000; i++) {
        user = new User();
            user = new User();
            user.setUserId(i);
            user.setUserName("jiangnan" + i);
            user.setAge(15);
            user.setEmail("ljnwsbs@163.com");
            user.setGender("male");
            user.setPhoneNumber("1234567890");
            list.add(user);
        }
        userWriteService.batchInsertUser(list);
    }

    @PostMapping("/batchDeleteUserById")
    public void batchDeleteUserById() {
        List<Integer> list =new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            if(i%5 == 0) {
                list.add(i);
            }
        }
        userWriteService.batchDeleteUserById(list);
    }

    @PostMapping("/deleteUserById")
    public void deleteUserById() {

        userWriteService.deleteUserById(1);
    }

}
